<?php
namespace Application\Api\Controller;

class Resource extends Base
{

    private function getService()
    {
        return $this->get(SERVICE_RESOURCE);
    }

    /*
        /Api/Resource/getGrade1List
    */
    public function getGrade1ListAction()
    {
        $data = $this->getRequestData();
        $categoryId = $data['categoryId'];
        $page = (int)tryGet($data, 'page', 1);
        $limit = (int)tryGet($data, 'limit', 10);
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $data['device']);
        $service = $this->getService();
        $temp = $service->getGrade1List($categoryId, $page, $limit, $userId);
        $list = $this->get(SERVICE_FILE)->getFileData($temp['list'], ['dry_author_avatar' => 1, 'dry_poster' => 1]);
        $send = [
            'oss_domain' => getOssDomain(),
            'list' => $list
        ];
        $send = addPageInformation($send, $page, $limit, $temp['count']);
        $this->sendSuccess($send);
    }

    /*
        /Api/Resource/getGrade123List
    */
    public function getGrade123ListAction()
    {
        $data = $this->getRequestData();
        $parentId = $data['parentId'];
        $service = $this->getService();
        $list = $service->getGrade123List($parentId);
        $map = [
            'audio' => '音频',
            'video' => '视频',
            'picture' => '图片',
            'file' => '文件',
            'text' => '文本'
        ];
        foreach($list as $k => $v){
            $v->dry_type_text = '';
            if(isset($map[$v->dry_type])){
                $v->dry_type_text = $map[$v->dry_type];
            }
            $list[$k] = $v;
        }
        $list = $this->get(SERVICE_FILE)->getFileData($list, ['dry_banner' => 1]);
        $send = [
            'oss_domain' => getOssDomain(),
            'list' => $list
        ];
        $this->sendSuccess($send);
    }

    /*
        /Api/Resource/getDetail
    */
    public function getDetailAction()
    {
        $data = $this->getRequestData();
        $id = $data['id'];
        $service = $this->getService();
        $rs = $service->one($id);
        $list = [$rs];
        $list = $this->get(SERVICE_FILE)->getFileData($list, ['dry_files' => 2]);
        $send = [
            'oss_domain' => getOssDomain(),
            'data' => $list[0]
        ];
        $this->sendSuccess($send);
    }

}